/**
 * @filename:TestController 2022-9-21
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wonder.admin.domain.Test;
import com.wonder.admin.service.TestService;
import com.wonder.admin.request.TestRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
/**   
 * 
 * <p>说明： 测试模块API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    2022-9-21
 *
 */
@Api(tags = "测试模块")
@RestController
@RequestMapping("/admin/Test")
public class TestController {
	
	@Autowired
	private TestService testService;
	
	@ApiOperation(value = "测试模块列表")
	@RequestMapping(value = "/index", method = RequestMethod.POST)
	public Map<String,Object> index(@RequestBody TestRequest testRequest) {
		Page<Test> page = new Page<Test>(testRequest.getPage(), testRequest.getLimit());
		IPage<Test> list = testService.selectList(testRequest, page);
		
		Map<String,Object> retMap=new HashMap<>();
		retMap.put("data",CommonPage.restPage(list));
		
  
		return AjaxMap.success(retMap);
	}
	
	/**
	 * @explain 修改排序开关
	 * @param   对象参数：Test
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/updateExt")
	@ApiOperation(value = "修改排序开关", notes = "作者：Wonder")
	public AjaxResult<Test> updateExt(@RequestBody Test entity){
		if (null!=entity) {
			boolean rsg = testService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success("update.success");
			  }else {
				  return AjaxResult.formatFailed("update.fail");
			  }
		}else {
			return AjaxResult.formatFailed("update.not.exist");
		}
	}
	
	/**
	 * @explain 添加
	 * @param   对象参数：Test
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/add")
	@ApiOperation(value = "添加", notes = "作者：Wonder")
	public AjaxResult<Test> add(@RequestBody Test entity){

		if (null!=entity) {
		    if(entity.getDropdown_mult()!=null)
		        entity.setDropdown_mult_table(StringUtils.join(entity.getDropdown_mult(),','));
		    if(entity.getImage_multi()!=null)
		        entity.setImage_multi_table(JSON.toJSONString(entity.getImage_multi()));
		    if(entity.getFile_multi()!=null)
		        entity.setFile_multi_table(JSON.toJSONString(entity.getFile_multi()));
		    if(entity.getKeyvalue_pair()!=null)
		        entity.setKeyvalue_pair_table(JSON.toJSONString(entity.getKeyvalue_pair()));
		    if(entity.getAddress()!=null)
		        entity.setAddress_table(StringUtils.join(entity.getAddress(),','));
		    if(entity.getTag_label()!=null)
		        entity.setTag_label_table(JSON.toJSONString(entity.getTag_label()));
		    
		    String validate=testService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = testService.save(entity);
			if (rsg) {
				  return AjaxResult.formatSuccess("insert.success");
			  }else {
				  return AjaxResult.formatFailed("insert.fail");
			  }
		}else {
			return AjaxResult.formatFailed("insert.parameter.error");
		}
	}
	
	/**
	 * @explain 复制添加
	 * @param   对象参数：Test
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/clone")
	@ApiOperation(value = "复制添加", notes = "作者：Wonder")
	public AjaxResult<Test> clone(@RequestBody Test entity){

		if (null!=entity) {
		    if(entity.getDropdown_mult()!=null)
		        entity.setDropdown_mult_table(StringUtils.join(entity.getDropdown_mult(),','));
		    if(entity.getImage_multi()!=null)
		        entity.setImage_multi_table(JSON.toJSONString(entity.getImage_multi()));
		    if(entity.getFile_multi()!=null)
		        entity.setFile_multi_table(JSON.toJSONString(entity.getFile_multi()));
		    if(entity.getKeyvalue_pair()!=null)
		        entity.setKeyvalue_pair_table(JSON.toJSONString(entity.getKeyvalue_pair()));
		    if(entity.getAddress()!=null)
		        entity.setAddress_table(StringUtils.join(entity.getAddress(),','));
		    if(entity.getTag_label()!=null)
		        entity.setTag_label_table(JSON.toJSONString(entity.getTag_label()));
		    
		    String validate=testService.validate(entity,"new");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);    
			boolean rsg = testService.save(entity);
			if (rsg) {
				  return AjaxResult.formatSuccess("insert.success");
			  }else {
				  return AjaxResult.formatFailed("insert.fail");
			  }
		}else {
			return AjaxResult.formatFailed("insert.parameter.error");
		}
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/getUpdateInfo")
	@ApiOperation(value = "获取测试模块对象", notes = "作者：Wonder")
	public AjaxResult<Test> getUpdateInfo(@RequestBody Map<String,Object> map){
	    if(map==null) AjaxResult.failed(MessageUtils.message("input.parameter.error"));
		Long id=Long.parseLong(map.get("id").toString());		
		 Test test=testService.getById(id);
		if(test.getDropdown_mult_table()!=null)
		    test.setDropdown_mult(StringUtils.split(test.getDropdown_mult_table(),','));
		if(test.getImage_multi_table()!=null)
		    test.setImage_multi(JSON.parseObject(test.getImage_multi_table()
		    ,new ArrayList<Map<String, Object>>().getClass()));
		if(test.getFile_multi_table()!=null)
		    test.setFile_multi(JSON.parseObject(test.getFile_multi_table()
		    ,new ArrayList<Map<String, Object>>().getClass()));
		if(test.getKeyvalue_pair_table()!=null)
		    test.setKeyvalue_pair(JSON.parseObject(test.getKeyvalue_pair_table()
		    ,new ArrayList<Map<String, Object>>().getClass()));
		if(test.getAddress_table()!=null)
		    test.setAddress(StringUtils.split(test.getAddress_table(),','));
		if(test.getTag_label_table()!=null)
		    test.setTag_label(JSON.parseObject(test.getTag_label_table()
		    ,new ArrayList<Map<String, Object>>().getClass()));
		
		if (null!=test ) {
			 return AjaxResult.success(test);
		}else {
			 return AjaxResult.failed(MessageUtils.message("search.not.exist"));
		}
		
	}
	
	/**
	 * @explain 修改
	 * @param   对象参数：Test
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "作者：Wonder")
	public AjaxResult<Test> update(@RequestBody Test entity){
		if (null!=entity) {
		    if(entity.getDropdown_mult()!=null)
		        entity.setDropdown_mult_table(StringUtils.join(entity.getDropdown_mult(),','));
		    if(entity.getImage_multi()!=null)
		        entity.setImage_multi_table(JSON.toJSONString(entity.getImage_multi()));
		    if(entity.getFile_multi()!=null)
		        entity.setFile_multi_table(JSON.toJSONString(entity.getFile_multi()));
		    if(entity.getKeyvalue_pair()!=null)
		        entity.setKeyvalue_pair_table(JSON.toJSONString(entity.getKeyvalue_pair()));
		    if(entity.getAddress()!=null)
		        entity.setAddress_table(StringUtils.join(entity.getAddress(),','));
		    if(entity.getTag_label()!=null)
		        entity.setTag_label_table(JSON.toJSONString(entity.getTag_label()));
		    String validate=testService.validate(entity,"update");
		    if(StringUtils.hasText(validate))
		         return AjaxResult.failed(validate);
			boolean rsg = testService.updateById(entity);
			if (rsg) {
				  return AjaxResult.formatSuccess("update.success");
			  }else {
				  return AjaxResult.formatFailed("update.fail");
			  }
		}else {
			return AjaxResult.formatFailed("update.not.exist");
		}
	}
	
	/**
	 * @explain 删除
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "作者：Wonder")
	public AjaxResult<Test> delete(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.formatFailed("search.not.exist");
		String ids=map.get("id").toString();
		ids=StringUtils.remove(ids, '[');
		ids=StringUtils.remove(ids, ']');
		String[] deleteIds=StringUtils.split(ids, ',');
		List<Long> idList=new ArrayList<>(); 
		for(int i=0;i<deleteIds.length;i++)
		{
			idList.add( Long.parseLong(deleteIds[i].trim()));//整数数组
		}

	    boolean rsg = testService.removeBatchByIds(idList);
	    if (rsg) {
		    return AjaxResult.formatSuccess("delete.success");
	    }else {
		   return AjaxResult.formatFailed("delete.fail");
	    }
		
	}
	
	/**
	 * @explain 查看对象明细  
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  Wonder
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "获取测试模块对象明细", notes = "作者：Wonder")
	public AjaxResult<Test> detail(@RequestBody Map<String,Object> map){
	    if(map==null) AjaxResult.formatFailed("input.parameter.error");
		Long id=Long.parseLong(map.get("id").toString());		
		Test test=testService.getById(id);
		if (null!=test ) {	
			 return AjaxResult.success(test);
		}else {
			 return AjaxResult.formatFailed("search.not.exist");
		}
		
	}
	
	
}